package com.example.sxt_practise.thread;

import java.util.concurrent.*;

/**
 * @author gaofeng
 * @date 2022年05月24日 22:55
 */
public class CallableTest implements Callable<Integer> {
    @Override
    public Integer call() {
        return 1 + 2;
    }

    public static void main(String[] args) throws Exception{
        FutureTask<Integer> task = new FutureTask<>(new CallableTest()) ;
        new Thread(task).start();
        System.out.println(task.get());

        ExecutorService service = Executors.newFixedThreadPool(1) ;
        // 结果会被缓存,提高效率
        Future<Integer> result = service.submit(new CallableTest());
        // 会阻塞,一直拿到结果为止, 可以放在最后,或者用异步
        Integer returnResult = result.get();
        System.out.println(returnResult);
    }
}
